package com.right.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.Tools.PageData;
import com.commons.AjaxResponse;
import com.google.gson.Gson;
import com.right.entity.Comment;
import com.right.entity.Foods;
import com.right.service.CommentService;
import com.right.service.impl.CommentServiceImpl;

/**
 * Servlet implementation class CommentServlet
 */
@WebServlet("/CommentServlet")
public class CommentServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	 CommentService service=new CommentServiceImpl();
	
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public CommentServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String op = "";
		if (request.getParameter("op")!=null) {
			op = request.getParameter("op");
		}
		if ("add".equals(op)) {
	
		} else if ("del".equals(op)) {
			doAjaxDel(request, response);
		} else if ("update".equals(op)) {
			// 修改
			doUpdateFoods(request, response);
		}else if ("selectByName".equals(op)) {
			
		}else if ("selectByPage".equals(op)) {
			doSelectByPage(request,response);
		}else if ("login".equals(op)) {
			
		}else if ("".equals(op)) {
			response.sendRedirect("main.jsp");
		}
	}
	private void doUpdateFoods(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 获取请求的参数
		int commentid = Integer.parseInt(request.getParameter("commentid"));
		String orderid = request.getParameter("orderid");
		String judge =request.getParameter("judge");
		String content = request.getParameter("content");
		String commenttime =request.getParameter("commenttime");
		// 构造一个foods对象
		Comment comment = new Comment(commentid, orderid, judge, content, commenttime);
		System.out.println(comment);
		boolean flag = service.updatecomment(comment);
		System.out.println(flag);
		// 根据flag的结果进行转发或者提示
		if (flag) {
			// 转发到查询菜品的界面
			request.setAttribute("msg", "修改成功！");
			request.getRequestDispatcher("s-comment.jsp").forward(request, response);
//				doSelectByPage(request, response);
		} else {
			// 修改失败给提示转发回到修改的页面
			request.setAttribute("msg", "修改失败！");
			request.getRequestDispatcher("s-comment.jsp").forward(request, response);
		}

	}
	private void doAjaxDel(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 1.设置响应头的contentType
		response.setContentType("application/json;charset=utf-8");

		// 先获取请求参数的值
		int commentid = 0;

		if (request.getParameter("commentid") != null) {
			commentid = Integer.parseInt(request.getParameter("commentid"));
		}

		// 2 调用 service的方法
		boolean flag = service.delcomment(commentid);

		AjaxResponse res = new AjaxResponse(flag + "", 200);
		// 3将list转换为json格式的字符串
		Gson gson = new Gson();
		String jsonString = gson.toJson(res);

		// 4 响应
		PrintWriter outPrintWriter = response.getWriter();
		outPrintWriter.print(jsonString);

		// 5.关闭
		outPrintWriter.close();
	}
	protected void doAdd(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	      String orderid =request.getParameter("orderid");
	      String judge =request.getParameter("judge");
	      String content=request.getParameter("content");
	      String  commenttime=request.getParameter("commenttime");
	      Comment comment=new Comment(orderid, judge, content, commenttime);
	        boolean  flag=service.addcomment(comment);
	        if (flag) {
		           //如果增加成功跳转到评价列表
                    
	        	
			}else {
				request.setAttribute("msg", "增加失败！请重新添加。");
				request.getRequestDispatcher("#").forward(request, response);
			}
	}
	protected void doDel(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		  int commentid   =Integer.parseInt(request.getParameter("commentid"));
		    boolean flag  =service.delcomment(commentid);
          if (flag) {
        		request.setAttribute("msg", "删除成功!");
        		//跳转到查看页面
        		
		}else {
			request.setAttribute("msg", "删除失败！");
			//跳转到增加评论的界面
			request.getRequestDispatcher("#").forward(request, response);
		}
	}
	protected void doSelectByName(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		 String keywords="";
		if (request.getParameter("keywords")!=null) {
			keywords=request.getParameter("keywords");
		} 
		  List<Comment>list=service.getComments(keywords);
		//传递list给jsp
		   request.setAttribute("list", list);
		//转发到评论列表
		 request.getRequestDispatcher("#").forward(request, response);
	   
	}
	protected void doSelectByPage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String keywords = "";
		// 获取请求参数
		if (request.getParameter("keywords") != null) {
			keywords = request.getParameter("keywords");
		}
		// 页码默认值在第一页
		int page = 1;
		//每页默认10条记录
		int pageSize =10;
		if (request.getParameter("page") != null) {
			page = Integer.parseInt(request.getParameter("page"));
		}

		if (request.getParameter("pageSize") != null) {
			pageSize = Integer.parseInt(request.getParameter("pageSize"));
		}
		//调用查询菜品的Service方法
		PageData<Comment> pData = service.getCommentByPage(page, pageSize, keywords);
		//传递PageData对象给jsp
		request.setAttribute("pData", pData);
		//转发回来的时候，将查询关键字也存储在request范围内
		request.setAttribute("keywords", keywords);
		//转发到评论列表  
		request.getRequestDispatcher("s-comment.jsp").forward(request, response);
	
	}
	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
